﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class leibie_updt : System.Web.UI.Page
{
// 2 请求参数成员变量列表
﻿    public string request_id = "";
﻿    public string request_mingcheng = "";



// 6.1 父列请求参数成员变量列表



    //初始化当前时间
    public string current_shijian = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim();
    //初始化当前日期
    public string current__riqi = DateTime.Now.ToString("yyyy-MM-dd").Trim();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null &&Request.QueryString["id"].ToString().Trim() != "")
        {
            request_id = Request.QueryString["id"].ToString().Trim();
        }
        else
        {
            Response.Write("<script>javascript:alert('对不起,信息id参数不能为空，请重新操作!');history.back();</script>");
            Response.End();
        }

        //request_tianjiashijian = current_shijian;
        //if (IsExistControl("tianjiashijian"))
        //{
        //    GetControl("tianjiashijian").Text = request_tianjiashijian;
        //}

        if (!IsPostBack)
        {

            string sql;
            sql = "select * from leibie where id=" + request_id;
            getdata(sql);
            //Response.Write("第一次,非回发");
        }
        else
        {
            //Response.Write("第一次以后,回发");
        }
    }


    //初始化数据需要修改的数据
    private void getdata(string sql)
    {
        DataSet result = new DataSet();
        result = new Class1().hsggetdata(sql);
        if (result != null)
        {
            if (result.Tables[0].Rows.Count > 0)
            {
//3 初始化参数列表
﻿
                if (IsExistControl("id"))
                {
                    GetControl("id").Text = result.Tables[0].Rows[0]["id"].ToString().Trim();
                }
                
﻿
                if (IsExistControl("mingcheng"))
                {
                    GetControl("mingcheng").Text = result.Tables[0].Rows[0]["mingcheng"].ToString().Trim();
                }
                



            }
        }
    }

    //执行修改功能实现代码
    protected void Update_Button_Click(object sender, EventArgs e)
    {

        string sql;

        sql = "update leibie set ";

//8 组织sql语句的属性与值
﻿
        if (IsExistControl("id"))
        {
            sql = sql + " id='" + GetControl("id").Text + "',";
        }
﻿
        if (IsExistControl("mingcheng"))
        {
            sql = sql + " mingcheng='" + GetControl("mingcheng").Text + "',";
        }




        sql = sql.Substring(0, sql.Length - 1);
        sql = sql +" where id=" + request_id;
        int result;
        result = new Class1().hsgexucute(sql);
        if (result == 1)
        {
            Response.Write("<script>javascript:alert('操作完成，继续操作！');history.back();</script>");
        }
        else
        {
            Response.Write("<script>javascript:alert('系统错误，请检查数据库设置问题');history.back();</script>");
        }
    }


    //选择父类信息
    private void addselect(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                //ListItem it0 = new ListItem();
                //it0.Value = "";
                //it0.Text = "全部";
                //((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {
                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //通过当前信息id作为父类id选择父类
    private void addselectbyId(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId, string FatherIdColumnValue)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " where " + FatherIdColumnName + "='" + FatherIdColumnValue + "'" + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                //ListItem it0 = new ListItem();
                //it0.Value = "";
                //it0.Text = "全部";
                //((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {

                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //获取父类信息
    private string getFatherSecondColumnValue(string FatherTable, string FatherSecondColumnName, string FatherIdColumnVaule)
    {
        string FatherSecondColumnValue = "";
        string sql;
        sql = "select " + FatherSecondColumnName + " from " + FatherTable + " where 1=1 and id='" + FatherIdColumnVaule + "' order by id desc";

        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {
                FatherSecondColumnValue = dsresult.Tables[0].Rows[0][0].ToString().Trim();
            }
        }

        return FatherSecondColumnValue;
    }



    public bool IsExistControl(string DropDownListControlId)  //判断控件是否存在
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public ITextControl GetControl(string ControlId)  //获取ITextControl控件对象
    {
        if (this.FindControl(ControlId) != null)
        {
            return (ITextControl)this.FindControl(ControlId);
        }
        else
        {
            return null;
        }
    }

    public TextBox GetTextBoxControl(string TextBoxControlId)  //获取TextBox控件对象
    {
        if (this.FindControl(TextBoxControlId) != null)
        {
            return (TextBox)this.FindControl(TextBoxControlId);
        }
        else
        {
            return null;
        }
    }

    public DropDownList GetDropDownListControl(string DropDownListControlId)  //获取DropDownList控件对象
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return (DropDownList)this.FindControl(DropDownListControlId);
        }
        else
        {
            return null;
        }
    }

}

